package cs2340.quarterblack.wheresmystuff;

/*
 * StackTest.java
 *
 * Version 1.0
 * Copyright 2011 BobSoft Inc
 */

import java.util.Date;
import java.util.LinkedList;
import java.util.List;


import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import cs2340.quarterblack.wheresmystuff.Item.Itemtype;

/**
 * @author Robert
 * @version 1.0
 *
 */
public class JUnitTests {
    /**
     * The test fixture
     */
	 Item item1;
	 Item item2;
	 Item item3;
	 Item item4;
     List<Item> list;
     
     //variable declaration for BK's test
     Account ac, admin;
     String id;
     String password;
     
     
     //Variables declaration for ZW's test
     Item zwItem1;
     Item zwItem2;
     Item zwItem3;
     Item zwItem4;
     Account currAccount;
     Storage storage;
    
     /**
     * This method is run before each test
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
        //FilterItems setup
        list = new LinkedList<Item>();
        item1 = new Item("fake id","item 1","none","Kansas",new Date(1994,4,30),Item.Category.PERSONAL);
        item2 = new Item("fake id","item 2","none","Kansas",new Date(1994,5,1),Item.Category.PERSONAL);
        item3 = new Item("fake id","item 3","none","Kansas",new Date(1993,7,6),Item.Category.FURNATURE);
        item4 = new Item("fake id","item 4","none","Kansas",new Date(2012,7,6),Item.Category.CLOTHING);
        list.add(item1);
        list.add(item2);
        list.add(item3);
        list.add(item4);
        
        //variables initialization for ByungKyu Kim's test
        id = "bkim61";
		password = "1234";
		ac = new Account(id, password);
		admin = new Account("admin", "admin", true);
		admin.addUser();
		ac.addUser();
		
		//variables initialization for Zhengyang Wu's test
		storage = StorageManager.getInstance();
		currAccount = new Account("bob","theman");
		zwItem1 = new Item("bob","item1","fakeitem","Atlanta",new Date(1993,3,3),Item.Category.PERSONAL);
		zwItem1.setType(Itemtype.LOST);
		zwItem2 = new Item("bob","item2","fakeitem","Atlanta",new Date(2013,3,3),Item.Category.FURNATURE);
		zwItem2.setType(Itemtype.FOUND);
		zwItem3 = new Item("bob","item3","fakeitem","Atlanta",new Date(2013,3,3),Item.Category.OTHER);
		zwItem3.setType(Itemtype.LOST);
		zwItem4 = new Item("bob","item3","fakeitem","Atlanta",new Date(2013,3,3),Item.Category.OTHER);
		zwItem4.setType(Itemtype.FOUND);
    }

    /**
     * Darrien Richmond's test
     * Test method for filterItems in account class.
     */
    @Test
    public void testAccountFilter() {
    	List<Item> filtered = Account.filterItems(list, "1",Account.SearchType.name);
    	Assert.assertTrue("name filter leaves out expected item",filtered.contains(item1));
    	Assert.assertFalse("name filter includes unexpected item",
    	filtered.contains(item2) || filtered.contains(item3) || 
    	filtered.contains(item4));
    	
        filtered = Account.filterItems(list, new Date(1994,5,1),Account.SearchType.date);
        Assert.assertTrue("date filter doesn't include the same date as entered date",filtered.contains(item2));
        Assert.assertFalse("date filter includes earlier dates",filtered.contains(item1) || filtered.contains(item3));
        Assert.assertTrue("date filter doesn't include later dates",filtered.contains(item4));
        
        filtered = Account.filterItems(list, "PErS",Account.SearchType.category);
        Assert.assertTrue("category filter doesn't include expected Items",(filtered.contains(item1) && filtered.contains(item2)));
        Assert.assertFalse("category filter contains unexpected items",filtered.contains(item3) || filtered.contains(item4));
    }
    
    /**
     * ByungKyu Kim's test
     * Test method for deleteAccount in account class.
     * Run test on following conditions:
     * 		1. delete an user account with user privilege
     * 		2. check for privilege on user account
     * 		3. check for privilege on admin account
     * 		4. delete an user account with admin privilege
     * 		5. check to see if an user account has been removed correctly
     * 		6. delete an admin account with admin privilege
     * 		7. check to see if an admin account has been removed correctly
     */
	@Test
	public void testDeleteAccount(){
		Assert.assertFalse(ac.deleteAccount(id));
		Assert.assertFalse(ac.getAdmin());
		Assert.assertTrue(admin.getAdmin());
		Assert.assertTrue(admin.deleteAccount(id));
		Assert.assertEquals(null, ac.getAccount());
		Assert.assertTrue(admin.deleteAccount("admin"));
		Assert.assertEquals(null, admin.getAccount());
	}

	/**
	 * Zhengyang Wu's test
	 * Test method for addItem in LocalStorage class.   
	 */
	@Test
	public void testAddItem() {
		storage.addItem(zwItem1);
		Assert.assertTrue("Did not add item in Lost list",storage.getUsersItems(currAccount, Itemtype.LOST).contains(zwItem1));
		storage.addItem(zwItem2);
		Assert.assertTrue("Did not add item in Found list",storage.getUsersItems(currAccount, Itemtype.FOUND).contains(zwItem2));
		Assert.assertFalse("Did not add item in the right list",storage.getUsersItems(currAccount, Itemtype.LOST).contains(zwItem2));
		Assert.assertFalse("Did not add item in the right list",storage.getUsersItems(currAccount, Itemtype.FOUND).contains(zwItem1));
		Assert.assertFalse("Show an item that has not been added to the list",storage.getUsersItems(currAccount, Itemtype.LOST).contains(zwItem3));
		Assert.assertFalse("Show an item that has not been added to the list",storage.getUsersItems(currAccount, Itemtype.FOUND).contains(zwItem4));
	}
}